home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / UD / GENTREE.ZIP / GENTREE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-26  |  1.5 KB  |  81 lines

  1. // GENTREE.CPP - GENERIC TREE Traversal utility
  2. // CopyRight (c) by Paul Kimmel 1994
  3. // All Rights Reserved.
  4. //
  5.  
  6.  
  7. #include <process.h>
  8. #include <stdlib.h>
  9. #include <dos.h>
  10. #include <string.h>
  11. #include <dir.h>
  12. #include <mem.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15.  
  16.  
  17. #include "gentree.h"
  18.  
  19.  
  20. extern unsigned _stklen = 65536U;
  21. char start[MAXPATH];
  22.  
  23. void (*func)();                                // generic function
  24. void traverse( char* pname = "*.*" );       // traversal function
  25. int  c_break(void);
  26.  
  27.  
  28.  
  29.  
  30. // The general tree starting point
  31. void GenTree( void (*f)(), char* startPath )
  32. {
  33.     func = f;
  34.  
  35.     ctrlbrk( c_break );                        // set ctrl-brk handler
  36.     memset( start, (int) 'P', MAXPATH );    // initialize path buffer
  37.     strcpy( start, startPath );                // copy path
  38.     traverse( start );                        // start recursive function
  39.  
  40. }
  41.  
  42. #define ABORT 0
  43.  
  44. int c_break(void)
  45. {
  46.     return(ABORT);
  47. }
  48.  
  49.  
  50. // Traverse the tree recursively
  51.  
  52. void traverse( char* pname )
  53. {
  54.    struct ffblk ffblk;
  55.  
  56.    if (kbhit()) exit;
  57.  
  58.    chdir( pname );            // change to subdirectory
  59.    func();
  60.  
  61.    int done;
  62.    done = findfirst("*.*", &ffblk, FA_DIREC);
  63.  
  64.    // get all of the sub-directories
  65.    while (!done){
  66.  
  67.       if( strcmp(ffblk.ff_name, "..") != 0 && strcmp(ffblk.ff_name, ".") != 0
  68.             && ffblk.ff_attrib == FA_DIREC)
  69.       {
  70.           printf("  %s\n", ffblk.ff_name);
  71.  
  72.           // call the generic function
  73.           traverse( ffblk.ff_name );
  74.       }
  75.       done = findnext(&ffblk);
  76.    }
  77.  
  78.    chdir("..");
  79. }
  80.  
  81.